package light.mvc.service.tba.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.mvc.dao.BaseDaoI;
import light.mvc.pageModel.base.PageFilter;
import light.mvc.model.tba.MamProductBO;
import light.mvc.pageModel.tba.MamProductVO;
import light.mvc.service.tba.MamProductServiceI;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class MamProductServiceImpl implements MamProductServiceI {

   @Autowired
   private BaseDaoI<MamProductBO> mamProductDao;

   @Override
   public void add(MamProductVO r) {
       MamProductBO t = new MamProductBO();
       t.setMaProduct(r.getMaProduct());
       t.setMaName(r.getMaName());
       t.setMaUrl(r.getMaUrl());
       t.setMaQrCode(r.getMaQrCode());
       t.setMaPwd(r.getMaPwd());
       t.setIsEnable(r.getIsEnable());
       mamProductDao.save(t);
   }

   @Override
   public void delete(Long id) {
       MamProductBO t = mamProductDao.get(MamProductBO.class, id);
       mamProductDao.delete(t);
   }

   @Override
   public void edit(MamProductVO r) {
       MamProductBO t = mamProductDao.get(MamProductBO.class, r.getId());
       t.setMaProduct(r.getMaProduct());
       t.setMaName(r.getMaName());
       t.setMaUrl(r.getMaUrl());
       t.setMaQrCode(r.getMaQrCode());
       t.setMaPwd(r.getMaPwd());
       t.setIsEnable(r.getIsEnable());
       mamProductDao.update(t);
   }
   
   @Override
   public List<MamProductVO> findByAll() {
       List<MamProductBO> listMamProductBO = mamProductDao.find("from MamProductBO where isEnable=1");
       List<MamProductVO> listMamProductVO = new ArrayList<MamProductVO>();
       for(MamProductBO mamProductBO:listMamProductBO){
    	   MamProductVO mamProductVO = new MamProductVO();
           BeanUtils.copyProperties(mamProductBO, mamProductVO);
           listMamProductVO.add(mamProductVO);
       }
       return listMamProductVO;
   }

   @Override
   public MamProductVO get(Long id) {
       MamProductBO t = mamProductDao.get(MamProductBO.class, id);
       MamProductVO r = new MamProductVO();
       r.setId(t.getId());
       r.setMaProduct(t.getMaProduct());
       r.setMaName(t.getMaName());
       r.setMaUrl(t.getMaUrl());
       r.setMaQrCode(t.getMaQrCode());
       r.setMaPwd(t.getMaPwd());
       r.setIsEnable(t.getIsEnable());
       return r;
   }

   @Override
   public List<MamProductVO> dataGrid(MamProductVO mamProduct, PageFilter ph) {
       List<MamProductVO> ul = new ArrayList<MamProductVO>();
       Map<String, Object> params = new HashMap<String, Object>();
       String hql = " from MamProductBO t ";
       List<MamProductBO> l = mamProductDao.find(hql + whereHql(mamProduct, params) + orderHql(ph), params, ph.getPage(), ph.getRows());
       for (MamProductBO t : l) {
           MamProductVO u = new MamProductVO();
           BeanUtils.copyProperties(t, u);
           ul.add(u);
       }
       return ul;
   }

   @Override
   public Long count(MamProductVO mamProduct, PageFilter ph) {
       Map<String, Object> params = new HashMap<String, Object>();
       String hql = " from MamProductBO t ";
       return mamProductDao.count("select count(*) " + hql + whereHql(mamProduct, params), params);
   }

   private String whereHql(MamProductVO mamProduct, Map<String, Object> params) {
       String hql = "";
       if (mamProduct != null) {
           hql += " where 1=1 ";
           if (mamProduct.getId() != null) {
               hql += " and t.name like :name";
               params.put("name", "%%" + mamProduct.getId() + "%%");
           }
       }
       return hql;
   }

   private String orderHql(PageFilter ph) {
       String orderString = "";
       if ((ph.getSort() != null) && (ph.getOrder() != null)) {
           orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
       }
       return orderString;
   }

}